c# .net

Control the brightness of image using c#

Control the brightness of image using c#, someone asked me to explain?

c# color rgb

In this article I will show you how to apply brightness of an image using c#.net. You can adjust the color components of the image from the input ranges between -255 and 255. You need to pass the bitmap image and input parameter (brightness) for the function. The function has image processing algorithms,it process with the RGB values and it will return the bitmap object as output.

   public static Bitmap SetBrightness(Bitmap btmap, int brightness)
        {
            Bitmap temp = (Bitmap)btmap;
            Bitmap bmap = (Bitmap)temp.Clone();
            if (brightness < -255) brightness = -255;
            if (brightness > 255) brightness = 255;
            Color c;
            for (int i = 0; i < bmap.Width; i++)
            {
                for (int j = 0; j < bmap.Height; j++)
                {
                    c = bmap.GetPixel(i, j);
                    int cR = c.R + brightness;
                    int cG = c.G + brightness;
                    int cB = c.B + brightness;
 
                   if (cR < 0) cR = 1;
                    if (cR > 255) cR = 255;
 
                    if (cG < 0) cG = 1;
                    if (cG > 255) cG = 255;
 
                    if (cB < 0) cB = 1;
                    if (cB > 255) cB = 255;
 
                    bmap.SetPixel(i, j, Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
                }
            }
            return (Bitmap)bmap.Clone();        }

 

Post your comments / questions